//
//  TopViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-29.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "TopViewController.h"
#import "TopNRViewController.h"
#import "VideoViewController.h"
#import "NetworkHandler.h"
#import "TopCell.h"
#import "TopNRCell.h"
#import "TopModel.h"
#import "UIImageView+WebCache.h"
#import "TopNRViewController.h"
#import "HeaderController.h"

@interface TopViewController ()<UIWebViewDelegate, UITableViewDataSource,UITableViewDelegate>


//models
@property(nonatomic, retain)TopModel * Top;
@property(nonatomic, retain)NSArray *myArr;
@property(nonatomic, retain)NSArray *myArr2;

//views
@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)UIImageView *myImageView;
@property(nonatomic, retain)UIButton *BigButton;
@property(nonatomic, retain)UIButton *bofangButton;

@end

@implementation TopViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array =[NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //[self takeId];
    
    [self takeId];
    self.view.backgroundColor = [UIColor purpleColor];
    
    //左button
    UIButton * leftb =[UIButton buttonWithType:UIButtonTypeSystem];
    leftb.frame =CGRectMake(20, 20, 20, 20);
    UIImage * img1 =[UIImage imageNamed:@"left.png"];
    [leftb setBackgroundImage:img1 forState:UIControlStateNormal];
    [leftb addTarget:self action:@selector(leftbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftb];
    
    
    //网络请求1.header页面的id
    NSString *headstr = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    [NetworkHandler connectioonNetGETDataWithURLString:headstr completion:^(id result) {
        
        self.headerstr =[[result valueForKey:@"topList"]valueForKey:@"id" ];
        
        
    }];



    //网络请求2
    NSString * str1 =@"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str1 completion:^(id result) {
        
        
        //大图 图片
        NSString * headerViewImageStr =[[result valueForKey:@"topList"]valueForKey:@"imageUrl"];
        UIImageView *tmpImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 210)];
        [tmpImageView setUserInteractionEnabled:YES];
        [tmpImageView setImageWithURL:[NSURL URLWithString:headerViewImageStr]];
        [self.myTableView setTableHeaderView:tmpImageView];
        [tmpImageView release];
        
        //点击大图按钮
        UIButton *HeaderButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 210)];
        [HeaderButton addTarget:self action:@selector(datuAction:) forControlEvents:UIControlEventTouchUpInside];
        [HeaderButton setTag:[[[result valueForKey:@"topList"] valueForKey:@"imageUrl"] integerValue]];
        [tmpImageView addSubview:HeaderButton];
        [HeaderButton release];
        self.bofangButton = HeaderButton;
        
        //大图上的标题
        self.headerViewTitleLabelStr = [[result valueForKey:@"topList"] valueForKey:@"title"];
        UILabel *headerViewTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, self.view.frame.size.width, 30)];
        [headerViewTitleLabel setText:_headerViewTitleLabelStr];
        // NSLog(@"%@", headerViewTitleLabelStr);
        [headerViewTitleLabel setBackgroundColor:[UIColor colorWithRed:128.0 / 255.0 green:0.0 / 255.0 blue:128.0 / 255.0 alpha:1.0]];
        headerViewTitleLabel.font = [UIFont systemFontOfSize:14];
        [headerViewTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [headerViewTitleLabel setTextColor:[UIColor colorWithRed:255.0 / 255.0 green:215.0 / 255.0 blue:0.0 / 255.0 alpha:1.0]];
        [headerViewTitleLabel setAlpha:0.7];
        [tmpImageView addSubview:headerViewTitleLabel];
        
    }];
   
    NSString *str = @"http://api.m.mtime.cn/TopList/MtimeTopList.api?pageIndex=1";
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        self.myArr = [result objectForKey:@"topLists"];
        [self.myTableView reloadData];
    }];
    

    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView registerClass:[TopCell class] forCellReuseIdentifier:@"top"];
    [self.view addSubview:tmpTableView];
    [tmpTableView release];
    self.myTableView = tmpTableView;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.myArr count];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"top";
    TopCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell) {
        cell = [[[TopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    //kvc
    NSDictionary *dic = [self.myArr objectAtIndex:indexPath.row];
    
    //图片
    //[cell.myImageView setImageWithURL:[NSURL URLWithString:self.Top.image]];
    
    //标题
    [cell.titleLabel setText:[dic objectForKey:@"topListNameCn" ]];

    //内容
    [cell.summaryLabel setText:[dic objectForKey:@"summary"]];
    
    return cell;
}

//取第一页的ID
- (void)takeId
{
    NSString * str =@"http://api.m.mtime.cn/TopList/MtimeTopList.api?pageIndex=1";
    NSURL * url =[NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
    NSArray *array = [dic objectForKey:@"topLists"];
    for (NSDictionary *myDic in array) {
        [_array addObject:[myDic objectForKey:@"id"]];
        
        
    }NSLog(@"dfsdfs = %@",array);

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row <= 9)
    {
        NSString *strID = [_array objectAtIndex:indexPath.row];
        TopNRViewController * topnr =[[TopNRViewController alloc] init];
        topnr.MyidStr =[NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetailsByRecommend.api?pageIndex=1&pageSubAreaID=%@",strID];
        
        // 传下一个页面title
        NSDictionary *dic = [self.myArr objectAtIndex:indexPath.row];
        topnr.value = [dic objectForKey:@"topListNameCn"];
        
        
        [self.navigationController pushViewController:topnr animated:YES];
    }
    else if(indexPath.row >9)
    {
        NSString *strID = [_array objectAtIndex:indexPath.row];
        TopNRViewController * topnr =[[TopNRViewController alloc] init];
        topnr.MyidStr =[NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=1&topListId=%@",strID];
        
        // 传下一个页面title
        NSDictionary *dic = [self.myArr objectAtIndex:indexPath.row];
        topnr.value = [dic objectForKey:@"topListNameCn"];
        
        
        [self.navigationController pushViewController:topnr animated:YES];
        
    }
}

- (void)leftbAction:(id)sender

{
    [self.navigationController popViewControllerAnimated:YES];
    
}

//header事件
- (void)datuAction:(id)sender
{
    HeaderController * headervc=[[HeaderController alloc]init];
    headervc.heaStr =[NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=1&topListId=%@",_headerstr];
    headervc.title=_headerViewTitleLabelStr;
    [self.navigationController pushViewController:headervc animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
